<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>MFixedPoint: src/Fp32f.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">MFixedPoint
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('Fp32f_8cpp_source.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Fp32f.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="Fp32f_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="preprocessor">#ifndef __cplusplus</span></div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="preprocessor">    #error Please build with C++ compiler</span></div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="preprocessor">#endif</span></div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;</div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="comment">//===============================================================================================//</span></div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="comment">//========================================= INCLUDES ============================================//</span></div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;<span class="comment">//===============================================================================================//</span></div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="Fp32f_8hpp.html">MFixedPoint/Fp32f.hpp</a>&quot;</span></div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="comment">//===============================================================================================//</span></div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="comment">//======================================== NAMESPACE ============================================//</span></div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;<span class="comment">//===============================================================================================//</span></div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;</div><div class="line"><a name="l00025"></a><span class="lineno"><a class="line" href="namespacemn.html">   25</a></span>&#160;<span class="keyword">namespace </span><a class="code" href="namespacemn.html">mn</a> {</div><div class="line"><a name="l00026"></a><span class="lineno"><a class="line" href="namespacemn_1_1MFixedPoint.html">   26</a></span>&#160;<span class="keyword">namespace </span>MFixedPoint {</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;<span class="comment">//===============================================================================================//</span></div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;<span class="comment">//==================================== PRIVATE VARIABLES ========================================//</span></div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;<span class="comment">//===============================================================================================//</span></div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;<span class="comment">//static const int32_t FIX16_2PI    = FloatToFix32&lt;16&gt;(6.28318530717958647692f);</span></div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;<span class="comment">//static const int32_t FIX16_R2PI = FloatToFix32&lt;16&gt;(1.0f/6.28318530717958647692f);</span></div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;</div><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;<span class="comment">//===============================================================================================//</span></div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;<span class="comment">//===================================== PUBLIC FUNCTIONS ========================================//</span></div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;<span class="comment">//===============================================================================================//</span></div><div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;</div><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;<span class="comment">/*</span></div><div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;<span class="comment">int32_t fixcos16(int32_t a) </span></div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;<span class="comment">{</span></div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;<span class="comment">    int32_t v;</span></div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;<span class="comment">    // reduce to [0,1]</span></div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;<span class="comment">    while (a &lt; 0) a += FIX16_2PI;</span></div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;<span class="comment">    a = FixMul&lt;16&gt;(a, FIX16_R2PI);</span></div><div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;<span class="comment">    a += 0x4000;</span></div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;<span class="comment"></span></div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;<span class="comment">    // now in the range [0, 0xffff], reduce to [0, 0xfff] </span></div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;<span class="comment">    a &gt;&gt;= 4;</span></div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;<span class="comment"></span></div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;<span class="comment">    v = (a &amp; 0x400) ? sin_tab[0x3ff - (a &amp; 0x3ff)] : sin_tab[a &amp; 0x3ff];</span></div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;<span class="comment">    v = FixMul&lt;16&gt;(v, 1 &lt;&lt; 16);</span></div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;<span class="comment">    return (a &amp; 0x800) ? -v : v;</span></div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;<span class="comment">}</span></div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;<span class="comment"></span></div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;<span class="comment">int32_t fixsin16(int32_t a)</span></div><div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;<span class="comment">{</span></div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;<span class="comment">    int32_t v;</span></div><div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;<span class="comment"></span></div><div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;<span class="comment">    // reduce to [0,1) </span></div><div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;<span class="comment">    while (a &lt; 0) a += FIX16_2PI;</span></div><div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;<span class="comment">    a = FixMul&lt;16&gt;(a, FIX16_R2PI);</span></div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;<span class="comment"></span></div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;<span class="comment">    // now in the range [0, 0xffff], reduce to [0, 0xfff] </span></div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;<span class="comment">    a &gt;&gt;= 4;</span></div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;<span class="comment"></span></div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;<span class="comment">    v = (a &amp; 0x400) ? sin_tab[0x3ff - (a &amp; 0x3ff)] : sin_tab[a &amp; 0x3ff];</span></div><div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;<span class="comment">    v = FixMul&lt;16&gt;(v, 1 &lt;&lt; 16);</span></div><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;<span class="comment">    return (a &amp; 0x800) ? -v : v;</span></div><div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;<span class="comment">}</span></div><div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;<span class="comment">*/</span></div><div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;</div><div class="line"><a name="l00073"></a><span class="lineno"><a class="line" href="namespacemn_1_1MFixedPoint.html#a5f8f94c6e7eca6ba8233ac7f0da880eb">   73</a></span>&#160;int32_t <a class="code" href="namespacemn_1_1MFixedPoint.html#a5f8f94c6e7eca6ba8233ac7f0da880eb">fixrsqrt16</a>(int32_t a)</div><div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;{</div><div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;    int32_t x;</div><div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;</div><div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;    <span class="keyword">static</span> <span class="keyword">const</span> uint16_t rsq_tab[] = { <span class="comment">// domain 0.5 .. 1.0-1/16 </span></div><div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;        0xb504, 0xaaaa, 0xa1e8, 0x9a5f, 0x93cd, 0x8e00, 0x88d6, 0x8432,</div><div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;    };</div><div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;</div><div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;    int32_t i, exp;</div><div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;    <span class="keywordflow">if</span> (a == 0) <span class="keywordflow">return</span> 0x7fffffff;</div><div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;    <span class="keywordflow">if</span> (a == (1&lt;&lt;16)) <span class="keywordflow">return</span> a;</div><div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;</div><div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;    exp = <a class="code" href="namespacemn_1_1MFixedPoint_1_1detail.html#a23c916cea5a4b151e69bff7e828610f2">detail::CountLeadingZeros</a>(a);</div><div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;    x = rsq_tab[(a&gt;&gt;(28-exp))&amp;0x7]&lt;&lt;1;</div><div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;</div><div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;    exp -= 16;</div><div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;    <span class="keywordflow">if</span> (exp &lt;= 0)</div><div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;        x &gt;&gt;= -exp&gt;&gt;1;</div><div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;    <span class="keywordflow">else</span></div><div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;        x &lt;&lt;= (exp&gt;&gt;1)+(exp&amp;1);</div><div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;    <span class="keywordflow">if</span> (exp&amp;1) x = FixMul&lt;16&gt;(x, rsq_tab[0]);</div><div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;</div><div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;</div><div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;    <span class="comment">// newton-raphson</span></div><div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;    <span class="comment">// x = x/2*(3-(a*x)*x)</span></div><div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;    i = 0;</div><div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;    <span class="keywordflow">do</span> {</div><div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;</div><div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;        x = FixMul&lt;16&gt;((x&gt;&gt;1),((1&lt;&lt;16)*3 - FixMul&lt;16&gt;(FixMul&lt;16&gt;(a,x),x)));</div><div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160;    } <span class="keywordflow">while</span>(++i &lt; 3);</div><div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;</div><div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;    <span class="keywordflow">return</span> x;</div><div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;}</div><div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;</div><div class="line"><a name="l00107"></a><span class="lineno"><a class="line" href="namespacemn_1_1MFixedPoint.html#ae4a8dc561a9ea6b45837015139604367">  107</a></span>&#160;<span class="keyword">static</span> <span class="keyword">inline</span> int32_t <a class="code" href="namespacemn_1_1MFixedPoint.html#ae4a8dc561a9ea6b45837015139604367">fast_div16</a>(int32_t a, int32_t b)</div><div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;{</div><div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;    <span class="keywordflow">if</span> ((b &gt;&gt; 24) &amp;&amp; (b &gt;&gt; 24) + 1) {</div><div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;        <span class="keywordflow">return</span> FixMul&lt;16&gt;(a &gt;&gt; 8, fixinv&lt;16&gt;(b &gt;&gt; 8));</div><div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;    } <span class="keywordflow">else</span> {</div><div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;        <span class="keywordflow">return</span> FixMul&lt;16&gt;(a, fixinv&lt;16&gt;(b));</div><div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;    }</div><div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;}</div><div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;</div><div class="line"><a name="l00117"></a><span class="lineno"><a class="line" href="namespacemn_1_1MFixedPoint.html#a67dbcc8f8829616f0928b35320c51d04">  117</a></span>&#160;int32_t <a class="code" href="namespacemn_1_1MFixedPoint.html#a67dbcc8f8829616f0928b35320c51d04">fixsqrt16</a>(int32_t a) </div><div class="line"><a name="l00118"></a><span class="lineno">  118</span>&#160;{</div><div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;    int32_t s;</div><div class="line"><a name="l00120"></a><span class="lineno">  120</span>&#160;    int32_t i;</div><div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;    s = (a + (1&lt;&lt;16)) &gt;&gt; 1;</div><div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;    <span class="comment">// 6 iterations to converge</span></div><div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;    <span class="keywordflow">for</span> (i = 0; i &lt; 6; i++)</div><div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;        s = (s + <a class="code" href="namespacemn_1_1MFixedPoint.html#ae4a8dc561a9ea6b45837015139604367">fast_div16</a>(a, s)) &gt;&gt; 1;</div><div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;    <span class="keywordflow">return</span> s;</div><div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;}</div><div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;</div><div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;} <span class="comment">// namespace MFixedPoint</span></div><div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;} <span class="comment">// namespace mn</span></div><div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;</div><div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;<span class="comment">// EOF</span></div><div class="ttc" id="namespacemn_1_1MFixedPoint_1_1detail_html_a23c916cea5a4b151e69bff7e828610f2"><div class="ttname"><a href="namespacemn_1_1MFixedPoint_1_1detail.html#a23c916cea5a4b151e69bff7e828610f2">mn::MFixedPoint::detail::CountLeadingZeros</a></div><div class="ttdeci">uint32_t CountLeadingZeros(uint32_t x)</div><div class="ttdef"><b>Definition:</b> <a href="Fp32f_8hpp_source.html#l00110">Fp32f.hpp:110</a></div></div>
<div class="ttc" id="namespacemn_html"><div class="ttname"><a href="namespacemn.html">mn</a></div><div class="ttdef"><b>Definition:</b> <a href="Fp32f_8cpp_source.html#l00025">Fp32f.cpp:25</a></div></div>
<div class="ttc" id="Fp32f_8hpp_html"><div class="ttname"><a href="Fp32f_8hpp.html">Fp32f.hpp</a></div><div class="ttdoc">Fast 32-bit fixed point library. </div></div>
<div class="ttc" id="namespacemn_1_1MFixedPoint_html_a67dbcc8f8829616f0928b35320c51d04"><div class="ttname"><a href="namespacemn_1_1MFixedPoint.html#a67dbcc8f8829616f0928b35320c51d04">mn::MFixedPoint::fixsqrt16</a></div><div class="ttdeci">int32_t fixsqrt16(int32_t a)</div><div class="ttdoc">Calculates squareroot. </div><div class="ttdef"><b>Definition:</b> <a href="Fp32f_8cpp_source.html#l00117">Fp32f.cpp:117</a></div></div>
<div class="ttc" id="namespacemn_1_1MFixedPoint_html_a5f8f94c6e7eca6ba8233ac7f0da880eb"><div class="ttname"><a href="namespacemn_1_1MFixedPoint.html#a5f8f94c6e7eca6ba8233ac7f0da880eb">mn::MFixedPoint::fixrsqrt16</a></div><div class="ttdeci">int32_t fixrsqrt16(int32_t a)</div><div class="ttdef"><b>Definition:</b> <a href="Fp32f_8cpp_source.html#l00073">Fp32f.cpp:73</a></div></div>
<div class="ttc" id="namespacemn_1_1MFixedPoint_html_ae4a8dc561a9ea6b45837015139604367"><div class="ttname"><a href="namespacemn_1_1MFixedPoint.html#ae4a8dc561a9ea6b45837015139604367">mn::MFixedPoint::fast_div16</a></div><div class="ttdeci">static int32_t fast_div16(int32_t a, int32_t b)</div><div class="ttdef"><b>Definition:</b> <a href="Fp32f_8cpp_source.html#l00107">Fp32f.cpp:107</a></div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_68267d1309a1af8e8297ef4c3efbcdba.html">src</a></li><li class="navelem"><a class="el" href="Fp32f_8cpp.html">Fp32f.cpp</a></li>
    <li class="footer">Generated on Wed Dec 13 2017 13:34:44 for MFixedPoint by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.13 </li>
  </ul>
</div>
</body>
</html>
